package surreal.triviamaster.gui;

import surreal.triviamaster.R;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Intent;
import android.os.Bundle;

public class LoadingScreen extends Activity{
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.loading_screen);
	 
	    // thread for displaying the SplashScreen
	    Thread splashTread = new Thread() {
	        @Override
	        public void run() {
	            try {
	                while(isMyServiceRunning()) {
	                	Thread.sleep(100);	                    
	                }
	            } catch(InterruptedException e) {
	                // do nothing
	            } finally {
	                finish();
	                Intent intent = new Intent(LoadingScreen.this, Game.class);
	                startActivity(intent);
	                stop();
	            }
	        }
	    };
	    splashTread.start();
	}
	
	/**
	 *@return true if TriviaMasterService is running;
	 */
	private boolean isMyServiceRunning() {
	    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
	    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
	        if ("surreal.triviamaster.utilities.TriviaMasterService".equals(service.service.getClassName())) {
	            return true;
	        }
	    }
	    return false;
	}
}
